library(dplyr)
library(readstata13)

## set working directory
## to replication folder

survey <- read.dta13("Harris_Data/Harris 1974 Job Satisfaction and Union Survey, study nos. 7487 and 7488/harris_s7487_spss.dta")

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- as.character(7487)

# study year (year)
survey$year <- 1974

# geographic data (urban)
survey$urban <- as.character(survey$S13)

# geographic data (region)
survey$region <- as.character(survey$S11)
class(survey$region)

# respondent head of household (hh)
summary(survey$F1)
survey$hh <- as.character(survey$F1)
summary(survey$hh)

# increasing inequality (inequality)
survey$inequality <- as.character(survey$P10_B)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)

survey$union.self <- as.character(survey$Q5A_1)
survey$union.self[survey$Q5A_4 == "Yes"] <- "Not Sure"

survey$union.other <- as.character(survey$Q5A_2)
survey$union.other[survey$Q5A_4 == "Yes"] <- "Not Sure"

# employment (employed)
survey$employed <- as.character(survey$F2A)

## employment self
survey$employed.self <- NA

# occupation
survey$occupation <- as.character(survey$F2B)

## occupation self
survey$occupation.self <- NA

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
summary(survey$F5)
survey$educ <- as.character(survey$F5)
str(survey$educ)                        

# household income (income)
survey$income <- as.character(survey$F6)

# age
survey$age <- as.character(survey$F4)

# race
survey$race <- as.character(survey$F9)

# politics (party)
survey$party <- as.character(survey$P5A)

# politics (ideology)
survey$ideology <- NA

# gender
survey$gender <- as.character(survey$F10)
summary(survey$gender)

# religion
survey$religion <- as.character(survey$F7)

## factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- as.character(survey$P10_A)
survey$dontcount <- as.character(survey$P10_C)
survey$leftout <- as.character(survey$P10_D)

## question place
survey$question_place <- "after party"

# subset
survey_7487 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                        "inequality", "inequality.variable", "union.self", "union.other",
                        "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                        "age", "race", "party", "ideology", "gender", "religion",
                        "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                        "question_place")]

# save file
#saveRDS(survey_7487, file = "Harris_Data/survey_7487.rds")
